<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Audit extends AdminController {

	function __construct() { 
		parent::__construct();
		$this -> load -> model("Audit_model");
		$this -> load -> model("Academia_job_model");
		$this->load->model("Academia_job_kh_model");
		$this-> load -> model("Subject_platform_model");	
		$this -> load -> model("Subject_platform_kh_model");	
		$this->load->model("Academic_report_model");
		$this->load->model("Academic_report_kh_model");
		$this->load->model("Awards_model");
		$this->load->model("Awards_kh_model");
		$this->load->model("Communication_model");
		$this->load->model("Communication_kh_model");
		$this->load->model("Horizontal_project_model");
		$this->load->model("Horizontal_project_kh_model");
		$this->load->model("Monograph_model");
		$this->load->model("Monograph_kh_model");
		$this->load->model("Paper_model");
		$this->load->model("Paper_kh_model");
		$this->load->model("Patent_data_model");
		$this->load->model("Patent_model");
		$this->load->model("Patent_kh_model");
		$this->load->model("Research_report_model");
		$this->load->model("Research_report_kh_model");
		$this->load->model("Science_conversion_model");
		$this->load->model("Science_conversion_kh_model");
		$this->load->model("Technical_standard_model");
		$this->load->model("Technical_standard_kh_model");
		$this->load->model("Vertical_project_model");
		$this->load->model("Vertical_project_kh_model");

		$this->load ->library('ProjectService/audit_service');
		$this->load->library('ProjectService/export_service');
	}

	public function Index() {
		$this -> load -> view('admin/auditList');
	}

	function getDatas($params = array(), $isExport = false) {
		$params = $_POST;
		$connar = array();
		if (!$isExport) {
			$connar = $this -> Audit_model -> pickPages($connar, $params);
		}
		
		$role=explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);
		/*if(in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role) && in_array(WebRole::BranchManage[XPHEnum::Value], $role)){
			$connar['where']='1=1';
		}else if(in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){
			$connar['where']=$this->sqlEscape('node=??',array(AuditNode::Second[XPHEnum::Value]));
		}else if(in_array(WebRole::BranchManage[XPHEnum::Value], $role)){
			$connar['where']=$this->sqlEscape('db_audit.dept_name=??',array(getSess()['dept_name']));
		}else{
			$connar['where']='1<>1';
		}

		if(isset($params['param'])){
			$connar['where'].=$this->sqlLikeEscape(" and data_name like '%??%'",array($params['param']));
		}
		if(isset($params['type']) && $params['type']!='-1'){
			$connar['where'].=$this->sqlEscape(" and type=??",array($params['type']));
		}
		if(isset($params['state']) && $params['state']!='-1'){
			$connar['where'].=$this->sqlEscape(" and state=??",array($params['state']));
		}*/
		if(in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role) && in_array(WebRole::BranchManage[XPHEnum::Value], $role)){
			// $where=' db_audit.is_del =0';
			$where=' is_del =0';
		}else if(in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){
			// $where=$this->sqlEscape('db_audit.node=?? and db_audit.is_del =0',array(AuditNode::Second[XPHEnum::Value]));
			$where=$this->sqlEscape(' node=?? and is_del =0',array(AuditNode::Second[XPHEnum::Value]));
		}else if(in_array(WebRole::BranchManage[XPHEnum::Value], $role)){
			// $where=$this->sqlEscape('db_audit.node=?? and db_audit.is_del =0 and db_audit.dept_name=??',array(AuditNode::First[XPHEnum::Value],getSess()['dept_name']));
			$where=$this->sqlEscape(' node=?? and is_del =0 and dept_name=??',array(AuditNode::First[XPHEnum::Value],getSess()['dept_name']));
		}else{
			if(in_array(WebRole::Admin[XPHEnum::Value], $role) || in_array(WebRole::Manage[XPHEnum::Value], $role)){
				// $where=' db_audit.is_del =0';
				$where=' is_del =0';
			}else{
				// $where=' db_audit.is_del =0 && 1<>1';
				$where=' is_del =0 && 1<>1';
			}
			
		}

		if(isset($params['commit'])){
			// $where.=$this->sqlLikeEscape(" and a1.username like '%??%'",array($params['commit']));
			$where.=$this->sqlLikeEscape(" and admin_name like '%??%'",array($params['commit']));
		}
		if(isset($params['param'])){
			// $where.=$this->sqlLikeEscape(" and db_audit.data_name like '%??%'",array($params['param']));
			$where.=$this->sqlLikeEscape(" and data_name1 like '%??%'",array($params['param']));
		}
		if(isset($params['type']) && $params['type']!='-1'){
			// $where.=$this->sqlEscape(" and db_audit.type=??",array($params['type']));
			$where.=$this->sqlEscape(" and type=??",array($params['type']));
		}

		if(isset($params['audit_type']) && $params['audit_type']!='-1'){
			// $where.=$this->sqlEscape(" and db_audit.audit_type=??",array($params['audit_type']));
			$where.=$this->sqlEscape(" and is_submit_cg=??",array($params['audit_type']));
		}
		
		if(isset($params['state']) && $params['state']!='-1'){
			// $where.=$this->sqlEscape(" and db_audit.state=??",array($params['state']));
			$where.=$this->sqlEscape(" and state=??",array($params['state']));
		}
	
		/*if (!$isExport) {
			$data = $this -> Audit_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Audit_model -> get_list_full($connar);
		}*/
		if(!$isExport){
			if(isset($params['pageNo']) && isset($params['pageSize'])){			
				$pageNo=$_POST['pageNo']-1;
				$pageSize=$_POST['pageSize'];
			}else{
				$pageNo='0';
				$pageSize='20';
			}
		}	

		$publicSql = "(
			SELECT
				`db_audit`.*,
				`a1`.`username` `admin_name`,
				`a2`.`username` `operator_name`,
			CASE
					`db_audit`.`type` 
					WHEN 1 THEN
					`db_paper`.`name` 
					WHEN 2 THEN
					`db_monograph`.`name` 
					WHEN 3 THEN
					`db_patent`.`name` 
					WHEN 4 THEN
					`db_awards`.`name` 
					WHEN 5 THEN
					`db_research_report`.`name` 
					WHEN 6 THEN
					`db_technical_standard`.`name` 
					WHEN 7 THEN
					`db_vertical_project`.`name` 
					WHEN 8 THEN
					`db_horizontal_project`.`name` 
					WHEN 9 THEN
					`db_science_conversion`.`name` 
					WHEN 10 THEN
					`db_communication`.`name` 
					WHEN 11 THEN
					`db_academic_report`.`name` 
					WHEN 12 THEN
					`db_academia_job`.`author` 
					WHEN 13 THEN
					`db_subject_platform`.`name` 
				END data_name1,
			CASE
					`db_audit`.`type` 
					WHEN 1 THEN
					`db_paper`.`is_submit_cg` 
					WHEN 2 THEN
					`db_monograph`.`is_submit_cg` 
					WHEN 3 THEN
					`db_patent`.`is_submit_cg` 
					WHEN 4 THEN
					`db_awards`.`is_submit_cg` 
					WHEN 5 THEN
					`db_research_report`.`is_submit_cg` 
					WHEN 6 THEN
					`db_technical_standard`.`is_submit_cg` 
					WHEN 7 THEN
					`db_vertical_project`.`is_submit_cg` 
					WHEN 8 THEN
					`db_horizontal_project`.`is_submit_cg` 
					WHEN 9 THEN
					`db_science_conversion`.`is_submit_cg` 
					WHEN 10 THEN
					`db_communication`.`is_submit_cg` 
					WHEN 11 THEN
					`db_academic_report`.`is_submit_cg` 
					WHEN 12 THEN
					`db_academia_job`.`is_submit_cg` 
					WHEN 13 THEN
					`db_subject_platform`.`is_submit_cg` 
				END is_submit_cg ,
			CASE
					`db_audit`.`type` 
					WHEN 1 THEN
					`db_paper`.`only_code` 
					WHEN 2 THEN
					`db_monograph`.`only_code` 
					WHEN 3 THEN
					`db_patent`.`only_code` 
					WHEN 4 THEN
					`db_awards`.`only_code` 
					WHEN 5 THEN
					`db_research_report`.`only_code` 
					WHEN 6 THEN
					`db_technical_standard`.`only_code` 
					WHEN 7 THEN
					`db_vertical_project`.`only_code` 
					WHEN 8 THEN
					`db_horizontal_project`.`only_code` 
					WHEN 9 THEN
					`db_science_conversion`.`only_code` 
					WHEN 10 THEN
					`db_communication`.`only_code` 
					WHEN 11 THEN
					''
					WHEN 12 THEN
					''
					WHEN 13 THEN
					`db_subject_platform`.`only_code` 
				END only_code 
			FROM `db_audit`
				LEFT JOIN `db_admin` `a1` ON `a1`.`id` = `db_audit`.`admin_id`
				LEFT JOIN `db_admin` `a2` ON `a2`.`id` = `db_audit`.`operator`
				LEFT JOIN `db_paper` ON `db_audit`.`data_id` = `db_paper`.`id` 
				AND `db_audit`.`type` = 1
				LEFT JOIN `db_monograph` ON `db_audit`.`data_id` = `db_monograph`.`id` 
				AND `db_audit`.`type` = 2
				LEFT JOIN `db_patent` ON `db_audit`.`data_id` = `db_patent`.`id` 
				AND `db_audit`.`type` = 3
				LEFT JOIN `db_awards` ON `db_audit`.`data_id` = `db_awards`.`id` 
				AND `db_audit`.`type` = 4
				LEFT JOIN `db_research_report` ON `db_audit`.`data_id` = `db_research_report`.`id` 
				AND `db_audit`.`type` = 5
				LEFT JOIN `db_technical_standard` ON `db_audit`.`data_id` = `db_technical_standard`.`id` 
				AND `db_audit`.`type` = 6
				LEFT JOIN `db_vertical_project` ON `db_audit`.`data_id` = `db_vertical_project`.`id` 
				AND `db_audit`.`type` = 7
				LEFT JOIN `db_horizontal_project` ON `db_audit`.`data_id` = `db_horizontal_project`.`id` 
				AND `db_audit`.`type` = 8
				LEFT JOIN `db_science_conversion` ON `db_audit`.`data_id` = `db_science_conversion`.`id` 
				AND `db_audit`.`type` = 9
				LEFT JOIN `db_communication` ON `db_audit`.`data_id` = `db_communication`.`id` 
				AND `db_audit`.`type` = 10
				LEFT JOIN `db_academic_report` ON `db_audit`.`data_id` = `db_academic_report`.`id` 
				AND `db_audit`.`type` = 11
				LEFT JOIN (
					SELECT id, dept, award_year, is_submit_cg, is_submit_kh, author.*  FROM db_academia_job
					LEFT JOIN ( SELECT group_concat( author_name ) author, group_concat( identity ) identity1, data_id  FROM db_author  WHERE db_author.type = 12  GROUP BY db_author.data_id 
					) author ON author.data_id = db_academia_job.id 
				) db_academia_job ON `db_audit`.`data_id` = `db_academia_job`.`id` 
			AND `db_audit`.`type` = 12
			LEFT JOIN `db_subject_platform` ON `db_audit`.`data_id` = `db_subject_platform`.`id` 
			AND `db_audit`.`type` = 13 
			)";

		if (!$isExport) {
			$data = $this -> Audit_model -> get_page_list($connar);
			// $data['ret']=$this -> Audit_model->query("SELECT `db_audit`.*, `a1`.`username` `admin_name`,	`a2`.`username` `operator_name`, CASE `db_audit`.`type` WHEN 1 THEN `db_paper`.`name` WHEN 2 THEN `db_monograph`.`name` WHEN 3 THEN `db_patent`.`name` WHEN 4 THEN `db_awards`.`name` WHEN 5 THEN `db_research_report`.`name` WHEN 6 THEN `db_technical_standard`.`name` WHEN 7 THEN `db_vertical_project`.`name` WHEN 8 THEN `db_horizontal_project`.`name` WHEN 9 THEN `db_science_conversion`.`name` WHEN 10 THEN `db_communication`.`name` WHEN 11 THEN `db_academic_report`.`name` WHEN 12 THEN `author`.`author` WHEN 13 THEN `db_subject_platform`.`name` END data_name1 FROM `db_audit` LEFT JOIN `db_admin` `a1` ON `a1`.`id` = `db_audit`.`admin_id` LEFT JOIN `db_admin` `a2` ON `a2`.`id` = `db_audit`.`operator` LEFT JOIN `db_paper` ON `db_audit`.`data_id` = `db_paper`.`id` AND `db_audit`.`type` = 1 LEFT JOIN `db_monograph` ON `db_audit`.`data_id` = `db_monograph`.`id` AND `db_audit`.`type` = 2 LEFT JOIN `db_patent` ON `db_audit`.`data_id` = `db_patent`.`id` AND `db_audit`.`type` = 3 LEFT JOIN `db_awards` ON `db_audit`.`data_id` = `db_awards`.`id` AND `db_audit`.`type` = 4 LEFT JOIN `db_research_report` ON `db_audit`.`data_id` = `db_research_report`.`id` AND `db_audit`.`type` = 5 LEFT JOIN `db_technical_standard` ON `db_audit`.`data_id` = `db_technical_standard`.`id` AND `db_audit`.`type` = 6 LEFT JOIN `db_vertical_project` ON `db_audit`.`data_id` = `db_vertical_project`.`id` AND `db_audit`.`type` = 7 LEFT JOIN `db_horizontal_project` ON `db_audit`.`data_id` = `db_horizontal_project`.`id` AND `db_audit`.`type` = 8 LEFT JOIN `db_science_conversion` ON `db_audit`.`data_id` = `db_science_conversion`.`id` AND `db_audit`.`type` = 9 LEFT JOIN `db_communication` ON `db_audit`.`data_id` = `db_communication`.`id` AND `db_audit`.`type` = 10 LEFT JOIN `db_academic_report` ON `db_audit`.`data_id` = `db_academic_report`.`id` AND `db_audit`.`type` = 11 LEFT JOIN (select group_concat(db_author.author_name)author,group_concat(db_author.identity) identity1,data_id FROM db_author WHERE db_author.type = 12 GROUP BY db_author.data_id ) author ON `db_audit`.`data_id` = `author`.`data_id` AND `db_audit`.`type` = 12 LEFT JOIN `db_subject_platform` ON `db_audit`.`data_id` = `db_subject_platform`.`id` AND `db_audit`.`type` = 13 WHERE ".$where." ORDER BY db_audit.state, db_audit.id DESC ".' limit '.$pageSize*$pageNo.','.$pageSize);
			// $count=$this -> Audit_model -> query("SELECT count(*) count FROM `db_audit` LEFT JOIN `db_admin` `a1` ON `a1`.`id` = `db_audit`.`admin_id` LEFT JOIN `db_admin` `a2` ON `a2`.`id` = `db_audit`.`operator` LEFT JOIN `db_paper` ON `db_audit`.`data_id` = `db_paper`.`id` AND `db_audit`.`type` = 1 LEFT JOIN `db_monograph` ON `db_audit`.`data_id` = `db_monograph`.`id` AND `db_audit`.`type` = 2 LEFT JOIN `db_patent` ON `db_audit`.`data_id` = `db_patent`.`id` AND `db_audit`.`type` = 3 LEFT JOIN `db_awards` ON `db_audit`.`data_id` = `db_awards`.`id` AND `db_audit`.`type` = 4 LEFT JOIN `db_research_report` ON `db_audit`.`data_id` = `db_research_report`.`id` AND `db_audit`.`type` = 5 LEFT JOIN `db_technical_standard` ON `db_audit`.`data_id` = `db_technical_standard`.`id` AND `db_audit`.`type` = 6 LEFT JOIN `db_vertical_project` ON `db_audit`.`data_id` = `db_vertical_project`.`id` AND `db_audit`.`type` = 7 LEFT JOIN `db_horizontal_project` ON `db_audit`.`data_id` = `db_horizontal_project`.`id` AND `db_audit`.`type` = 8 LEFT JOIN `db_science_conversion` ON `db_audit`.`data_id` = `db_science_conversion`.`id` AND `db_audit`.`type` = 9 LEFT JOIN `db_communication` ON `db_audit`.`data_id` = `db_communication`.`id` AND `db_audit`.`type` = 10 LEFT JOIN `db_academic_report` ON `db_audit`.`data_id` = `db_academic_report`.`id` AND `db_audit`.`type` = 11 LEFT JOIN (select group_concat(db_author.author_name)author,group_concat(db_author.identity) identity1,data_id FROM db_author WHERE db_author.type = 12 GROUP BY db_author.data_id ) author ON `db_audit`.`data_id` = `author`.`data_id` AND `db_audit`.`type` = 12 LEFT JOIN `db_subject_platform` ON `db_audit`.`data_id` = `db_subject_platform`.`id` AND `db_audit`.`type` = 13 WHERE ".$where." ORDER BY db_audit.state, db_audit.id DESC ");
			// $data['count']=$count[0]['count'];
			// $data['totalPage']=ceil($data['count']/$pageSize);
				$data['ret']=$this -> Audit_model->query("select * from ".$publicSql." as auditlist WHERE ".$where." ORDER BY state, id DESC ".' limit '.$pageSize*$pageNo.','.$pageSize);
				// echo $this->db->last_query();
				$count=$this -> Audit_model->query("select count(*) count from ".$publicSql." as auditlist WHERE ".$where." ORDER BY state, id DESC ");
			// $count=$this -> Audit_model -> query("SELECT count(*) count FROM `db_audit` LEFT JOIN `db_admin` `a1` ON `a1`.`id` = `db_audit`.`admin_id` LEFT JOIN `db_admin` `a2` ON `a2`.`id` = `db_audit`.`operator` LEFT JOIN `db_paper` ON `db_audit`.`data_id` = `db_paper`.`id` AND `db_audit`.`type` = 1 LEFT JOIN `db_monograph` ON `db_audit`.`data_id` = `db_monograph`.`id` AND `db_audit`.`type` = 2 LEFT JOIN `db_patent` ON `db_audit`.`data_id` = `db_patent`.`id` AND `db_audit`.`type` = 3 LEFT JOIN `db_awards` ON `db_audit`.`data_id` = `db_awards`.`id` AND `db_audit`.`type` = 4 LEFT JOIN `db_research_report` ON `db_audit`.`data_id` = `db_research_report`.`id` AND `db_audit`.`type` = 5 LEFT JOIN `db_technical_standard` ON `db_audit`.`data_id` = `db_technical_standard`.`id` AND `db_audit`.`type` = 6 LEFT JOIN `db_vertical_project` ON `db_audit`.`data_id` = `db_vertical_project`.`id` AND `db_audit`.`type` = 7 LEFT JOIN `db_horizontal_project` ON `db_audit`.`data_id` = `db_horizontal_project`.`id` AND `db_audit`.`type` = 8 LEFT JOIN `db_science_conversion` ON `db_audit`.`data_id` = `db_science_conversion`.`id` AND `db_audit`.`type` = 9 LEFT JOIN `db_communication` ON `db_audit`.`data_id` = `db_communication`.`id` AND `db_audit`.`type` = 10 LEFT JOIN `db_academic_report` ON `db_audit`.`data_id` = `db_academic_report`.`id` AND `db_audit`.`type` = 11 LEFT JOIN (select group_concat(db_author.author_name)author,group_concat(db_author.identity) identity1,data_id FROM db_author WHERE db_author.type = 12 GROUP BY db_author.data_id ) author ON `db_audit`.`data_id` = `author`.`data_id` AND `db_audit`.`type` = 12 LEFT JOIN `db_subject_platform` ON `db_audit`.`data_id` = `db_subject_platform`.`id` AND `db_audit`.`type` = 13 WHERE ".$where." ORDER BY db_audit.state, db_audit.id DESC ");
			$data['count']=$count[0]['count'];
			$data['totalPage']=ceil($data['count']/$pageSize);

		} else {
			$data['ret']=$this -> Audit_model->query("select * from ".$publicSql." as auditlist WHERE ".$where." ORDER BY state, id DESC ");
			// $data['ret']=$this -> Audit_model->query("SELECT `db_audit`.*, `a1`.`username` `admin_name`,	`a2`.`username` `operator_name`, CASE `db_audit`.`type` WHEN 1 THEN `db_paper`.`name` WHEN 2 THEN `db_monograph`.`name` WHEN 3 THEN `db_patent`.`name` WHEN 4 THEN `db_awards`.`name` WHEN 5 THEN `db_research_report`.`name` WHEN 6 THEN `db_technical_standard`.`name` WHEN 7 THEN `db_vertical_project`.`name` WHEN 8 THEN `db_horizontal_project`.`name` WHEN 9 THEN `db_science_conversion`.`name` WHEN 10 THEN `db_communication`.`name` WHEN 11 THEN `db_academic_report`.`name` WHEN 12 THEN `author`.`author` WHEN 13 THEN `db_subject_platform`.`name` END data_name1 FROM `db_audit` LEFT JOIN `db_admin` `a1` ON `a1`.`id` = `db_audit`.`admin_id` LEFT JOIN `db_admin` `a2` ON `a2`.`id` = `db_audit`.`operator` LEFT JOIN `db_paper` ON `db_audit`.`data_id` = `db_paper`.`id` AND `db_audit`.`type` = 1 LEFT JOIN `db_monograph` ON `db_audit`.`data_id` = `db_monograph`.`id` AND `db_audit`.`type` = 2 LEFT JOIN `db_patent` ON `db_audit`.`data_id` = `db_patent`.`id` AND `db_audit`.`type` = 3 LEFT JOIN `db_awards` ON `db_audit`.`data_id` = `db_awards`.`id` AND `db_audit`.`type` = 4 LEFT JOIN `db_research_report` ON `db_audit`.`data_id` = `db_research_report`.`id` AND `db_audit`.`type` = 5 LEFT JOIN `db_technical_standard` ON `db_audit`.`data_id` = `db_technical_standard`.`id` AND `db_audit`.`type` = 6 LEFT JOIN `db_vertical_project` ON `db_audit`.`data_id` = `db_vertical_project`.`id` AND `db_audit`.`type` = 7 LEFT JOIN `db_horizontal_project` ON `db_audit`.`data_id` = `db_horizontal_project`.`id` AND `db_audit`.`type` = 8 LEFT JOIN `db_science_conversion` ON `db_audit`.`data_id` = `db_science_conversion`.`id` AND `db_audit`.`type` = 9 LEFT JOIN `db_communication` ON `db_audit`.`data_id` = `db_communication`.`id` AND `db_audit`.`type` = 10 LEFT JOIN `db_academic_report` ON `db_audit`.`data_id` = `db_academic_report`.`id` AND `db_audit`.`type` = 11 LEFT JOIN (select group_concat(db_author.author_name)author,group_concat(db_author.identity) identity1,data_id FROM db_author WHERE db_author.type = 12 GROUP BY db_author.data_id ) author ON `db_audit`.`data_id` = `author`.`data_id` AND `db_audit`.`type` = 12 LEFT JOIN `db_subject_platform` ON `db_audit`.`data_id` = `db_subject_platform`.`id` AND `db_audit`.`type` = 13 WHERE ".$where." ORDER BY db_audit.state, db_audit.id DESC ");
			// echo $this->db->last_Query();
			// 暂未有导出
		}
		foreach ($data['ret'] as $k => $v) {
			$data['ret'] [$k]['type_char']=(new ResultsType)->show($v['type']);
			$data['ret'] [$k]['audit_type_char']=(new AuditType)->show($v['is_submit_cg']);
			$data['ret'] [$k]['state_char']=(new AuditState)->show($v['state']);
			$data['ret'] [$k]['node_char']=(new AuditNode)->show($v['node']);
			$data['ret'] [$k]['created_char']=date('Y-m-d H:i:s',$v['created']);
			if($v['audit_time']){
				$data['ret'] [$k]['audit_time_char']=date('Y-m-d H:i:s',$v['audit_time']);
			}else{
				$data['ret'] [$k]['audit_time_char']='--';	
			}
			if(in_array(WebRole::BranchManage[XPHEnum::Value], $role) || in_array(WebRole::Sscientifictaff[XPHEnum::Value], $role)){
				if(in_array(WebRole::BranchManage[XPHEnum::Value], $role) && $v['node']==AuditNode::Second[XPHEnum::Value]){
					$data['ret'] [$k]['can_audit']=false;
				}else{
					$data['ret'] [$k]['can_audit']=true;
				}
			
			}else{
				$data['ret'] [$k]['can_audit']=false;
			}
			if(in_array(WebRole::Admin[XPHEnum::Value], $role) || in_array(WebRole::Manage[XPHEnum::Value], $role)){
				$data['ret'][$k]['is_delete'] = true;
			}else{
				$data['ret'][$k]['is_delete'] = false;
			}
		   
			switch ($v['type']) {
				case ResultsType::First[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Paper/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Second[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Monograph/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Third[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Patent/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Fourth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Awards/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Fifth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Research/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Sixth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Technical/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Seventh[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Portrait/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Eighth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Transverse/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Ninth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Science/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Tenth[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Communication/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Eleventh[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Academic/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Twelfth[XPHEnum::Value]:
					$academia = $this->Academia_job_model->get_single(array('id'=>$v['data_id']));
					if($academia){						
						$data['ret'][$k]['data_name1'] = $v['data_name1'].$academia['dept'].$academia['post'];
					}
					$data['ret'][$k]['show_edit']="/Admin/Academia/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				case ResultsType::Thirteen[XPHEnum::Value]:
					$data['ret'][$k]['show_edit']="/Admin/Subjectplatform/edit/".$v['data_id']."?is_audit=".getSess()['id']."&node=".$v['node']."&state=".$v['state'];
					break;
				
			}
		}
		if (!$isExport) {
			echo json_encode($data);
		} else {
			//导出excel
            $new_list = array();	//需要导出的数据组
            foreach ($data["ret"] as $key => $value) {
                $new_list[$key]['xuhao'] = $key+1;//第一列序号
                $new_list[$key]['type_char'] = $value['type_char']; //成果类型
                $new_list[$key]['audit_type_char'] = $value['audit_type_char'];//审核类型
                $new_list[$key]['data_name1'] = $value['data_name1'];//成果名称
                $new_list[$key]['node_char'] = $value['node_char'];//审核节点
                $new_list[$key]['state_char'] = $value['state_char'];//状态
                $new_list[$key]['created_char'] = $value['created_char'];//提交审核时间
                $new_list[$key]['admin_name'] = $value['admin_name'];//提交人
                $new_list[$key]['operator_name'] = $value['operator_name'];//审核人
                $new_list[$key]['audit_time_char'] = $value['audit_time_char'];//审核时间
            }
            //导出excel名称
            $name = '审核信息';

			
            //导出表头名称
            $tableth = '序号,成果类型,审核类型,成果名称,审核节点,状态,提交审核时间,提交人,审核人,审核时间';

            //new_list导出数据列,title 导出表名称,tableth表头名称

            $desc =  $this->export_service->export_excel($new_list, $name, $tableth, 2, 3, $name);

            //返回地址信息
            $director = $desc['director'];
            $file_name = $desc['file_name'];
            exit(retJson('返回地址', true, $director.$file_name));
			// return $data["ret"];
		}
	}

	// 审核通过
	function pass(){
		if(!isset($_POST['type']) || !isset($_POST['audit_id'])){
			exit(retJson('参数错误',false));
		}
		if(!isset($_POST['is_kaohe'])){
			$return=$this->audit_service->pass($_POST['type'],$_POST['audit_id'],false);
		}else{
			$return=$this->audit_service->pass($_POST['type'],$_POST['audit_id'],true);
		}
		
		exit($return);		
	}

	//将考核数据转化为成果奖励
	function convert(){
		if(!isset($_POST['type']) || !isset($_POST['audit_id'])){
			exit(retJson('参数错误',false));
		}
		$return=$this->audit_service->convert($_POST['type'],$_POST['audit_id']);
		exit($return);		
	}

	// 审核拒绝
	function refused(){
		if(!isset($_POST['type']) || !isset($_POST['audit_id'])){
			exit(retJson('参数错误',false));
		}
		$return=$this->audit_service->refused($_POST['type'],$_POST['audit_id'],$_POST['remark']);
		exit($return);
	}

	// 删除审核记录并删除关联成果
	function delete($id){
		$auditList = $this -> Audit_model -> get_single(array('id'=>$id));
		if($auditList){
			$this->Audit_model->update(array('id'=>$id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
			//科研处审核并且是已通过的成果则删除对应的成果表中的成果,使其成果搜索中不出现此成果
			if($auditList['node'] == AuditNode::Second[XPHEnum::Value] && $auditList['state'] == AuditState::Pass[XPHEnum::Value]){
				$data_id = $auditList['data_id'];
				//修改成果原表状态
				switch ($auditList['type']) {
					case ResultsType::First[XPHEnum::Value]://论文
						$this->Paper_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('lw_'.$data_id));
						$kh = $this->Paper_kh_model->get_single_full($connar1);
						$this->Paper_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						break;

					case ResultsType::Second[XPHEnum::Value]://专注
	
						$this->Monograph_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('zz_'.$data_id));
						$kh = $this->Monograph_kh_model->get_single_full($connar1);
						if($kh){
							$this->Monograph_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}

						break;
					
					case ResultsType::Third[XPHEnum::Value]://专利
						$this->Patent_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('zl_'.$data_id));
						$kh = $this->Patent_kh_model->get_single_full($connar1);
						if($kh){
							$this->Patent_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Fourth[XPHEnum::Value]://获奖
						$this->Awards_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('hj_'.$data_id));
						$kh = $this->Awards_kh_model->get_single_full($connar1);
						if($kh){
							$this->Awards_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Fifth[XPHEnum::Value]://研究报告
						$this->Research_report_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('yjbg_'.$data_id));
						$kh = $this->Research_report_kh_model->get_single_full($connar1);
						if($kh){
							$this->Research_report_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Sixth[XPHEnum::Value]://技术标准
						$this->Technical_standard_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('jsbz_'.$data_id));
						$kh = $this->Technical_standard_kh_model->get_single_full($connar1);
						if($kh){
							$this->Technical_standard_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Seventh[XPHEnum::Value]://纵向
						$this->Vertical_project_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('new_uuid=?? ', array('zxkt_'.$data_id));
						$kh = $this->Vertical_project_kh_model->get_single_full($connar1);
						if($kh){
							$this->Vertical_project_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Eighth[XPHEnum::Value]://横向
						$this->Horizontal_project_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('new_uuid=?? ', array('hxkt_'.$data_id));
						$kh = $this->Horizontal_project_kh_model->get_single_full($connar1);
						if($kh){
							$this->Horizontal_project_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Ninth[XPHEnum::Value]://科技转化
						$this->Science_conversion_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('kjzh_'.$data_id));
						$kh = $this->Science_conversion_kh_model->get_single_full($connar1);
						if($kh){
							$this->Science_conversion_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Tenth[XPHEnum::Value]://学术交流会
						$this->Communication_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('xsjlh_'.$data_id));
						$kh = $this->Communication_kh_model->get_single_full($connar1);
						if($kh){
							$this->Communication_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Eleventh[XPHEnum::Value]://学术报告
						$this->Academic_report_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('xsbg_'.$data_id));
						$kh = $this->Academic_report_kh_model->get_single_full($connar1);
						if($kh){
							$this->Academic_report_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					
					case ResultsType::Twelfth[XPHEnum::Value]://学术兼职
						$this->Academia_job_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('xsjz_'.$data_id));
						$kh = $this->Academia_job_kh_model->get_single_full($connar1);
						if($kh){
							$this->Academia_job_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;
					case ResultsType::Thirteen[XPHEnum::Value]://学科平台
						$this->Subject_platform_model->update(array('id'=>$data_id,'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						$connar1['where'] = $this->sqlEscape('uuid=?? ', array('xkpt_'.$data_id));
						$kh = $this->Subject_platform_kh_model->get_single_full($connar1);
						if($kh){
							$this->Subject_platform_kh_model->update(array('id'=>$kh['id'],'is_del'=>CommonWhether::Whether[XPHEnum::Value]));
						}
						break;	
				}
			}
			

				exit(retJson('删除成功',true));
		}else{
			exit(retJson('参数错误',false));
		}
	}

	
}